Fix handling of key ranges ("a .. d")
authorJustin Burkett <justin@burkett.cc>
Thu, 9 Feb 2017 15:27:41 +0000 (10:27 -0500)
committerJustin Burkett <justin@burkett.cc>
Thu, 9 Feb 2017 15:29:30 +0000 (10:29 -0500)
When the last key in the key sequence is a range, extract the whole range
instead of just the final key.

Fixes #161

which-key-tests.el
which-key.el

index aa50ec01ae7d08aca2c3ddaf8c9d2e08bd205bee..1312f832c8d9fcd035bfb6a8ed8890ca7a301b84 100644 (file)
@@ -99,7 +99,7 @@
              '("SPC t 2" . "[ ] test mode")))))
 
 (ert-deftest which-key-test--maybe-replace-multiple ()
-  "Test `which-key-allow-multiple-replacements'. See #156"
+  "Test `which-key-allow-multiple-replacements'. See #156."
   (let ((which-key-replacement-alist
          '(((nil . "helm") . (nil . "HLM"))
            ((nil . "projectile") . (nil . "PRJTL"))))
              (which-key--maybe-replace '("C-c C-c" . "helm-projectile-x"))
              '("C-c C-c" . "HLM-PRJTL-x")))))
 
+(ert-deftest which-key-test--key-extraction ()
+  "Test `which-key--extract-key'. See #161."
+  (should (equal (which-key--extract-key "SPC a") "a"))
+  (should (equal (which-key--extract-key "C-x a") "a"))
+  (should (equal (which-key--extract-key "<left> b a") "a"))
+  (should (equal (which-key--extract-key "<left> a .. c") "a .. c"))
+  (should (equal (which-key--extract-key "M-a a .. c") "a .. c")))
+
 (provide 'which-key-tests)
 ;;; which-key-tests.el ends here
index 4a386a1fd735f7b2a527af2d162c0bdd9c596660..e9c2170ebdd4c21cd02ef03b088cda2d64532efb 100644 (file)
@@ -1439,6 +1439,14 @@ ORIGINAL-DESCRIPTION is the description given by
                             str))))))
     desc))
 
+(defun which-key--extract-key (key-str)
+  "Pull the last key (or key range) out of KEY-STR."
+  (save-match-data
+    (let ((key-range-regexp "\\`.*\\([^ \t]+ \\.\\. [^ \t]+\\)\\'"))
+      (if (string-match key-range-regexp key-str)
+          (match-string 1 key-str)
+        (car (last (split-string key-str " ")))))))
+
 (defun which-key--format-and-replace (unformatted)
   "Take a list of (key . desc) cons cells in UNFORMATTED, add
 faces and perform replacements according to the three replacement
@@ -1451,7 +1459,7 @@ alists. Returns a list (key separator description)."
       (let* ((key (car key-binding))
              (orig-desc (cdr key-binding))
              (group (which-key--group-p orig-desc))
-             (keys (which-key--current-key-string key))
+             (keys (concat (which-key--current-key-string) " " key))
              (local (eq (which-key--safe-lookup-key local-map (kbd keys))
                         (intern orig-desc)))
              (hl-face (which-key--highlight-face orig-desc))
@@ -1459,7 +1467,7 @@ alists. Returns a list (key separator description)."
         (when (consp key-binding)
           (push
            (list (which-key--propertize-key
-                  (car (last (split-string (car key-binding) " "))))
+                  (which-key--extract-key (car key-binding)))
                  sep-w-face
                  (which-key--propertize-description
                   (cdr key-binding) group local hl-face orig-desc))